package com.yanqu.road.dao.impl.activity.suitang.data;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangCityEvent;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangWarType;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSuiTangCityEventDaoImpl extends TempDataObjectDao<CrossSuiTangCityEvent> {
    public static String getCrossSuiTangCityEventSql = "select * from `t_c_suitang_city_event_data` where `activityId`=? and `groupId`=?";
    public Map<Integer, Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCityEvent>>>> getCrossSuiTangCityEvent(int activityId, int groupId) {
        Map<Integer, Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCityEvent>>>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSuiTangCityEventSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSuiTangCityEvent city = parse(rs);
                Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCityEvent>>> warTypeMap = map.get(city.getRound());
                if (warTypeMap == null) {
                    warTypeMap = new ConcurrentHashMap<>();
                    warTypeMap.put(eSuiTangWarType.WAR_TYPE_YIJUN, new ConcurrentHashMap<>());
                    warTypeMap.put(eSuiTangWarType.WAR_TYPE_XIAOQI, new ConcurrentHashMap<>());
                    warTypeMap.put(eSuiTangWarType.WAR_TYPE_JINJUN, new ConcurrentHashMap<>());
                    map.put(city.getRound(), warTypeMap);
                }
                Map<Integer, CrossSuiTangCityEvent> groupMap = warTypeMap.get(city.getWarType()).get(city.getActGroupId());
                if (groupMap == null) {
                    groupMap = new ConcurrentHashMap<>();
                    warTypeMap.get(city.getWarType()).put(city.getActGroupId(), groupMap);
                }
                groupMap.put(city.getCityId(), city);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossSuiTangCityEventSql = "update `t_c_suitang_city_event_data` set buffId=?,refreshTime=?,endTime=?,UnionId=? where activityId=? and groupId=? and actGroupId=? and round=? and warType=? and cityId=?";
    @Override
    protected boolean updateData(CrossSuiTangCityEvent crossSuiTangCityEvent) {
        crossSuiTangCityEvent.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getBuffId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCityEvent.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCityEvent.getEndTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangCityEvent.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getActGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangCityEvent.getWarType())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getCityId()));
        if (getDb().executeNoneQuery(updateCrossSuiTangCityEventSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSuiTangCityEventSql = "insert into `t_c_suitang_city_event_data`(activityId,groupId,actGroupId,round,warType,cityId,buffId,refreshTime,endTime,UnionId) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSuiTangCityEvent crossSuiTangCityEvent) {
        crossSuiTangCityEvent.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getActGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangCityEvent.getWarType())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getCityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangCityEvent.getBuffId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCityEvent.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangCityEvent.getEndTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangCityEvent.getUnionId()));
        if (getDb().executeNoneQuery(insertCrossSuiTangCityEventSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSuiTangCityEvent parseData(ResultSet rs) throws SQLException {
        CrossSuiTangCityEvent crossSuiTangCityEvent = new CrossSuiTangCityEvent();
        crossSuiTangCityEvent.setActivityId(rs.getInt("activityId"));
        crossSuiTangCityEvent.setGroupId(rs.getInt("groupId"));
        crossSuiTangCityEvent.setActGroupId(rs.getInt("actGroupId"));
        crossSuiTangCityEvent.setRound(rs.getInt("round"));
        crossSuiTangCityEvent.setWarType(JSON.parseObject(rs.getString("warType"), eSuiTangWarType.class));
        crossSuiTangCityEvent.setCityId(rs.getInt("cityId"));
        crossSuiTangCityEvent.setBuffId(rs.getInt("buffId"));
        crossSuiTangCityEvent.setRefreshTime(rs.getLong("refreshTime"));
        crossSuiTangCityEvent.setEndTime(rs.getLong("endTime"));
        crossSuiTangCityEvent.setUnionId(rs.getString("UnionId"));

        return crossSuiTangCityEvent;
    }
}
